package com.junlong0716.enjoylearning.ui

import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.adapter.StudyHistoryAdapter
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.StudyHistoryEntity
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.getknowledgedemo.decoration.DividerItemDecoration
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_study_history.*
import kotlinx.android.synthetic.main.view_title_bar.*
import org.json.JSONObject


class StudyHistoryActivity : AppCompatActivity(), View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener {
    private lateinit var studyHistoryAdapter: StudyHistoryAdapter
    private lateinit var data: ArrayList<StudyHistoryEntity>
    private var isLoadingMore = false
    private var currentPage = 1
    private var onePageCount = 10

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_study_history)
        initView()
    }

    private fun initView() {

        requestData()
        rl_back.setOnClickListener(this)
        tv_title.text = "历史记录"
        data = ArrayList()
        studyHistoryAdapter = StudyHistoryAdapter(data, R.layout.item_study_history)
        rv_history.layoutManager = LinearLayoutManager(this)
        rv_history.adapter = studyHistoryAdapter
        rv_history.addItemDecoration(DividerItemDecoration(this,DividerItemDecoration.VERTICAL_LIST,2,ContextCompat.getColor(this,R.color.common_transparent10)))
        studyHistoryAdapter.setOnLoadMoreListener(this, rv_history)
        //studyHistoryAdapter.emptyView = LayoutInflater.from(this).inflate(R.layout.view_empty, null)
    }

    private fun requestData() {
        val map = HashMap<String, Int>()
        map["currentPage"] = currentPage
        map["onePageCount"] = onePageCount
        map["userId"] = MyApp.instance().userAccount!!.id
        val json = JSONObject(map)
        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_LEARN_HISTORY)
                .tag(this)
                .upJson(json)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getInt("code")
                        if (code == 200) {
                            val data = jsonObj.getJSONObject("data")
                            val list = data.getJSONArray("list")
                            if (isLoadingMore){
                                if (list.length() == 0){
                                    studyHistoryAdapter.loadMoreEnd()
                                }else{
                                    val moreData = ArrayList<StudyHistoryEntity>()
                                    for (i in 0 until list.length()) {
                                        val knowledgeTitle = list.getJSONObject(i).getString("knowledgeTitle")
                                        val createDate = list.getJSONObject(i).getLong("createDate")
                                        moreData.add(StudyHistoryEntity(0, knowledgeTitle, 0, createDate))
                                    }
                                    studyHistoryAdapter.addData(moreData)
                                    studyHistoryAdapter.loadMoreComplete()
                                }
                            }else{
                                if (list.length() == 0) {
                                    studyHistoryAdapter.emptyView = LayoutInflater.from(this@StudyHistoryActivity).inflate(R.layout.view_empty, null)
                                } else {
                                    for (i in 0 until list.length()) {
                                        val knowledgeTitle = list.getJSONObject(i).getString("knowledgeTitle")
                                        val createDate = list.getJSONObject(i).getLong("createDate")
                                        this@StudyHistoryActivity.data.add(StudyHistoryEntity(0, knowledgeTitle, 0, createDate))
                                    }
                                    studyHistoryAdapter.notifyDataSetChanged()
                                   // onLoadMoreRequested()
                                }
                            }
                        }
                    }

                    override fun onError(response: Response<String>?) {
                        ToastUtils.showShort("网络有问题加载失败！")
                    }
                })
    }

    override fun onClick(v: View?) {
        when (v!!.id) {
            R.id.rl_back -> {
                finish()
            }
            else -> {
            }
        }
    }

    override fun onLoadMoreRequested() {
        isLoadingMore = true
        currentPage += 1
        requestData()
    }
}
